package com.example.vue20220402.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.vue20220402.dao.DoctorMapper;
import com.example.vue20220402.dao.OrderMapper;
import com.example.vue20220402.dao.PatientMapper;
import com.example.vue20220402.dao.SectionMapper;
import com.example.vue20220402.model.*;
import com.example.vue20220402.pojo.Power;
import com.example.vue20220402.pojo.Usersinfo;
import com.example.vue20220402.service.IOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.vue20220402.vo.Constant;
import com.example.vue20220402.vo.ResultData;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author rock
 * @since 2022-04-14
 */
@Service
public class OrderServiceImpl implements IOrderService {

    @Resource
    OrderMapper orderMapper;

    @Override
    public ResultData getPatientOrder(Order order) {
        ResultData rd = new ResultData();
        QueryWrapper<Order> qw = new QueryWrapper();
        qw.orderByDesc("order_id");//  查询最近的信息
        if(order.getUserId() != null){
            qw.eq("p.user_id",order.getUserId());
        }
        Page<Order> page = new Page<>(order.getPage(),order.getRow());
        //Page<Order> orderPage = orderMapper.selectOrderPage(page, qw);
        IPage<Order> list = orderMapper.selectOrderPage(page, qw);
        rd.setResult(list);
        return rd;
    }

    @Resource
    SectionMapper sectionMapper;
    @Resource
    DoctorMapper doctorMapper;

    @Override
    public ResultData getDoctors() {
        ResultData rd = new ResultData();
        List<Section> secs = sectionMapper.selectList(null);// 查询所有的科室
        List<Doctor> doctors = doctorMapper.selectList(null);//查询所有的医生
        List<ResultSecDoc> rsd = new ArrayList<>();
        for(Section s : secs){  // 把科室加入到第一层
            ResultSecDoc r = new ResultSecDoc();
            r.setLabel(s.getSectionName());
            r.setValue(s.getSectionId());
            rsd.add(r);
        }
        for(ResultSecDoc r : rsd){
            r.setChildren(new ArrayList<>());// 第一层 初始化 children 集合
            for(Doctor d : doctors){ // 把医生放到对应的children属性里
                if(d.getSectionId() == r.getValue()){
                    ResultSecDoc r2 = new ResultSecDoc();
                    r2.setLabel(d.getDoctorName());
                    r2.setValue(d.getDoctorId());
                    r.getChildren().add(r2);
                }
            }
        }
        rd.setResult(rsd);
        return rd;
    }

    @Override
    public ResultData getTimeQuan(Order order) {
        ResultData rd = new ResultData();
        List<TimeQuan> timeQuans = orderMapper.orderMapper(order);
        rd.setResult(timeQuans);
        return rd;
    }
}
